/******************************************************************************
 Copyright (c) 2012-2025 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

******************************************************************************/

#include "csr_synergy.h"
#include "csr_msgconv.h"
#include "csr_pmem.h"
#include "csr_util.h"
#include "csr_bt_cm_free_handcoded.h"
#ifndef EXCLUDE_CSR_BT_CM_MODULE
#include "csr_bt_cm_prim.h"
#ifdef EXCLUDE_CSR_BT_SC_MODULE
#include "csr_bt_cm_private_prim.h"
#endif
#include "csr_bt_cm_main.h"
#include "csr_bt_config.h"
#include "dmlib.h"

void CsrBtCmFreeHandcoded(void *message)
{
    CsrBtCmPrim *prim = (CsrBtCmPrim *) message;
    switch(*prim)
    {
#ifndef EXCLUDE_CSR_BT_CM_BNEP_CONNECT_ACCEPT_REQ
        case CSR_BT_CM_BNEP_CONNECT_ACCEPT_REQ:
        {
            CsrBtCmBnepConnectAcceptReqPrimFree(prim);
            break;
        }
#endif /* EXCLUDE_CSR_BT_CM_BNEP_CONNECT_ACCEPT_REQ */
#ifndef EXCLUDE_CSR_BT_CM_BNEP_CONNECT_REQ
        case CSR_BT_CM_BNEP_CONNECT_REQ:
        {
            CsrBtCmBnepConnectReqPrimFree(prim);
            break;
        }
#endif /* EXCLUDE_CSR_BT_CM_BNEP_CONNECT_REQ */
#ifndef EXCLUDE_CSR_BT_CM_SM_IO_CAPABILITY_REQUEST_RES
        case CSR_BT_CM_SM_IO_CAPABILITY_REQUEST_RES:
        {
            CsrBtCmSmIoCapabilityRequestResPrimFree(prim);
            break;
        }
#endif /* EXCLUDE_CSR_BT_CM_SM_IO_CAPABILITY_REQUEST_RES */
#ifdef EXCLUDE_CSR_BT_SC_MODULE
#ifndef EXCLUDE_CSR_BT_CM_DATABASE_REQ
        case CSR_BT_CM_DATABASE_REQ:
        {
            CsrBtCmDatabaseReqPrimFree(prim);
            break;
        }
#endif /* EXCLUDE_CSR_BT_CM_DATABASE_REQ */
#ifndef EXCLUDE_CSR_BT_CM_DATABASE_CFM
        case CSR_BT_CM_DATABASE_CFM:
        {
            CsrBtCmDatabaseCfmPrimFree(prim);
            break;
        }
#endif /* EXCLUDE_CSR_BT_CM_DATABASE_CFM */
#endif /* EXCLUDE_CSR_BT_SC_MODULE */
#ifndef EXCLUDE_CM_SDC_SERVICE_SEARCH_ATTR_REQ
        case CM_SDC_SERVICE_SEARCH_ATTR_REQ:
        {
            CmSdcServiceSearchAttrReq *p = message;
            CsrPmemFree(p->svcSearchAttrInfoList);
            p->svcSearchAttrInfoList = NULL;
            break;
        }
#endif /* EXCLUDE_CM_SDC_SERVICE_SEARCH_ATTR_REQ */
#ifndef EXCLUDE_CSR_BT_CM_SM_ADD_DEVICE_REQ
        case CSR_BT_CM_SM_ADD_DEVICE_REQ:
        {
            CsrBtCmSmAddDeviceReq *p = message;
            CsrPmemFree(p->keys);
            p->keys = NULL;
            break;
        }
#endif /* EXCLUDE_CSR_BT_CM_SM_ADD_DEVICE_REQ */
#ifdef CSR_BT_INSTALL_EXTENDED_ADVERTISING
        case CSR_BT_CM_EXT_ADV_SET_DATA_REQ:
        {
            CmExtAdvSetDataReq *p = message;
            CsrUint8 i;
            for (i = 0; i < CSR_ARRAY_SIZE(p->data); i++)
            {
                CsrPmemFree(p->data[i]);
                p->data[i] = NULL;
            }
            break;
        }
        
        case CSR_BT_CM_EXT_ADV_SET_SCAN_RESP_DATA_REQ:
        {
            CmExtAdvSetScanRespDataReq *p = message;
            CsrUint8 i;
            for (i = 0; i < CSR_ARRAY_SIZE(p->data); i++)
            {
                CsrPmemFree(p->data[i]);
                p->data[i] = NULL;
            }
            break;
        }
#endif /* CSR_BT_INSTALL_EXTENDED_ADVERTISING */
#ifdef CSR_BT_INSTALL_ISOC_SUPPORT
        case CSR_BT_CM_ISOC_BIG_CREATE_SYNC_CFM:
        {
            CmIsocBigCreateSyncCfm *p = message;
            CsrPmemFree(p->bis_handles);
            p->bis_handles = NULL;
            break;
        }
#endif /* CSR_BT_INSTALL_ISOC_SUPPORT */
        case CSR_BT_CM_ISOC_CONFIGURE_CIG_REQ:
        {
            CsrBtCmIsocConfigureCigReq *p = message;
            CsrUint8 i;

            for (i = 0; i < CSR_ARRAY_SIZE(p->cis_config); i++)
            {
                CsrPmemFree(p->cis_config[i]);
                p->cis_config[i]=NULL;
            }
            break;
        }
#ifndef EXCLUDE_CSR_BT_CM_ISOC_CIS_CONNECT_REQ
        case CSR_BT_CM_ISOC_CIS_CONNECT_REQ:
        {
            CsrBtCmIsocCisConnectReq *p = message;
            CsrUint8 i;

            for (i = 0; i < CSR_ARRAY_SIZE(p->cis_conn); i++)
            {
                CsrPmemFree(p->cis_conn[i]);
                p->cis_conn[i] = NULL;
            }
            break;
        }
#endif /* EXCLUDE_CSR_BT_CM_ISOC_CIS_CONNECT_REQ */
#ifndef EXCLUDE_CSR_BT_CM_ISOC_CONFIGURE_CIG_TEST_REQ
        case CSR_BT_CM_ISOC_CONFIGURE_CIG_TEST_REQ:
        {
            CsrBtCmIsocConfigureCigTestReq *p = message;
            CsrUint8 i;

            for (i = 0; i < CSR_ARRAY_SIZE(p->cis_test_config); i++)
            {
                CsrPmemFree(p->cis_test_config[i]);
                p->cis_test_config[i] = NULL;
            }
            break;
        }
#endif /* EXCLUDE_CSR_BT_CM_ISOC_CONFIGURE_CIG_TEST_REQ */
#ifdef INSTALL_CM_EXT_SET_CONN_PARAM
        case CM_DM_EXT_SET_CONN_PARAMS_REQ:
        {
            CmDmExtSetConnParamsReq *p = message;
            CsrUint8 i;

            for (i = 0; i < CSR_ARRAY_SIZE(p->initPhys); i++)
            {
                CsrPmemFree(p->initPhys[i]);
                p->initPhys[i] = NULL;
            }
            break;
        }
#endif /* INSTALL_CM_EXT_SET_CONN_PARAM */
        case CM_DM_HCI_CONFIGURE_DATA_PATH_REQ:
        {
            CmDmHciConfigureDataPathReq *p = message;
            CsrUint8 i;

            for (i = 0; i < CSR_ARRAY_SIZE(p->vendorSpecificConfig); i++)
            {
                CsrPmemFree(p->vendorSpecificConfig[i]);
                p->vendorSpecificConfig[i]=NULL;
            }
            break;
        }

        case CSR_BT_CM_PERIODIC_ADV_SET_DATA_REQ:
        {
            CsrBtCmPeriodicAdvSetDataReq *p = message;
            CsrUint8 i;

            for (i = 0; i < CSR_ARRAY_SIZE(p->data); i++)
            {
                CsrPmemFree(p->data[i]);
                p->data[i] = NULL;
            }
            break;
        }

        case CM_SM_CONFIG_REQ:
        {
            CmSmConfigReq *p = message;
            CsrPmemFree(p->params);
            p->params = NULL;
            break;
        }

#ifdef INSTALL_CM_LE_ENCRYPT_DATA_FEATURE
        case CM_ENCRYPT_DATA_REQ:
        {
            CmEncryptDataReq *p = message;
            CsrUint8 i;

            for (i = 0; i < CSR_ARRAY_SIZE(p->data); i++)
            {
                CsrPmemFree(p->data[i]);
                p->data[i] = NULL;
            }
            break;
        }

        case CM_ENCRYPT_DATA_CFM:
        {
            CmEncryptDataCfm *p = message;
            CsrUint8 i;

            for (i = 0; i < CSR_ARRAY_SIZE(p->data); i++)
            {
                CsrPmemFree(p->data[i]);
                p->data[i] = NULL;
            }
            break;
        }
#endif /* INSTALL_CM_LE_ENCRYPT_DATA_FEATURE */

        case CSR_BT_CM_ISOC_CREATE_BIG_CFM:
        {
            CsrBtCmIsocCreateBigCfm *p = message;
            CsrPmemFree(p->bis_handles);
            p->bis_handles = NULL;
            break;
        }

        case CSR_BT_CM_ISOC_CREATE_BIG_TEST_CFM:
        {
            CsrBtCmIsocCreateBigTestCfm *p = message;
            CsrPmemFree(p->bis_handles);
            p->bis_handles = NULL;
            break;
        }
#ifdef CSR_BT_INSTALL_EXTENDED_SCANNING
        case CSR_BT_CM_EXT_SCAN_FILTERED_ADV_REPORT_IND:
        {
            CmExtScanFilteredAdvReportInd *p = message;
#ifndef CSR_STREAMS_ENABLE
            CsrPmemFree(p->data);
#else
            /* Data will be freed in CM_EXT_SCAN_FILTERED_ADV_REPORT_DONE_IND message handling.
             * Application should not use this data pointer after this. So, setting it to NULL. */
#endif
            p->data = NULL;
            break;
        }
#endif /* CSR_BT_INSTALL_EXTENDED_SCANNING */
#ifdef CSR_BT_INSTALL_PERIODIC_SCANNING
            case CSR_BT_CM_PERIODIC_SCAN_SYNC_ADV_REPORT_IND:
            {
                CsrBtCmPeriodicScanSyncAdvReportInd *p = message;
#ifdef CSR_STREAMS_ENABLE
                /* Data will be freed in CM_PERIODIC_SCAN_SYNC_ADV_REPORT_DONE_IND message handling.
                 * Application should not use this data pointer after this. So, setting it to NULL. */
#else
                CsrPmemFree(p->data);
#endif /* CSR_STREAMS_ENABLE */
                p->data = NULL;
                break;
            }
#endif /* CSR_BT_INSTALL_PERIODIC_SCANNING */
#ifndef EXCLUDE_CM_EXT_SCAN_FILTERED_ADV_REPORT_DONE_IND
            case CM_EXT_SCAN_FILTERED_ADV_REPORT_DONE_IND:
            {
                CmExtScanFilteredAdvReportDoneInd *p = message;
#ifdef CSR_STREAMS_ENABLE
                /* Data will be freed in CM_EXT_SCAN_FILTERED_ADV_REPORT_DONE_IND message handling.
                 * Application should not use this data pointer after this. So, setting it to NULL. */
#else
                CsrPmemFree(p->data);
#endif /* CSR_STREAMS_ENABLE */
                p->data = NULL;
                break;
            }
#endif /* EXCLUDE_CM_EXT_SCAN_FILTERED_ADV_REPORT_DONE_IND */
#ifndef EXCLUDE_CM_PERIODIC_SCAN_SYNC_ADV_REPORT_DONE_IND
            case CM_PERIODIC_SCAN_SYNC_ADV_REPORT_DONE_IND:
            {
                CmPeriodicScanSyncAdvReportDoneInd *p = message;
#ifdef CSR_STREAMS_ENABLE
                /* Data will be freed in CM_PERIODIC_SCAN_SYNC_ADV_REPORT_DONE_IND message handling.
                 * Application should not use this data pointer after this. So, setting it to NULL. */
#else
                CsrPmemFree(p->data);
#endif /* CSR_STREAMS_ENABLE */
                p->data = NULL;
                break;
            }
#endif /* EXCLUDE_CM_PERIODIC_SCAN_SYNC_ADV_REPORT_DONE_IND */
#ifndef EXCLUDE_CSR_BT_CM_SM_READ_LOCAL_OOB_DATA_CFM
            case CSR_BT_CM_SM_READ_LOCAL_OOB_DATA_CFM:
            {
                CsrBtCmSmReadLocalOobDataCfm *p = message;
                if(p->status == HCI_SUCCESS)
                {
                    CsrPmemFree(p->oob_hash_c);
                    CsrPmemFree(p->oob_rand_r);
                    p->oob_hash_c = NULL;
                    p->oob_rand_r = NULL;
                }
                break;
            }
#endif /* CSR_BT_CM_SM_READ_LOCAL_OOB_DATA_CFM */
            case CSR_BT_CM_SM_KEYS_IND:
            {
                CsrBtCmSmKeysInd *p = message;
                dm_free_sm_keys(p->keys);
                break;
            }
#ifdef INSTALL_CHANNEL_SOUNDING_SUPPORT
            case CM_LE_CS_SUBEVENT_RESULT_IND:
            {
                CmLeCsSubeventResultInd *ind = message;
#ifdef CSR_STREAMS_ENABLE
                cmInstanceData_t *cmData = CmGetInstanceDataPtr();
                SourceDrop(cmData->csEventSource, sizeof(DM_ULP_CS_SUBEVENT_RESULT_IND_T) + ind->stepsDataLen);
                CmLeCsSubeventResultRspSend();
#else /* !CSR_STREAMS_ENABLE */
                CsrPmemFree(ind->steps);
#endif /* CSR_STREAMS_ENABLE */
                break;
            } 

            case CM_LE_CS_SUBEVENT_RESULT_CONTINUE_IND:
            {
                CmLeCsSubeventResultContinueInd *ind = message;
#ifdef CSR_STREAMS_ENABLE
                cmInstanceData_t *cmData = CmGetInstanceDataPtr();
                SourceDrop(cmData->csEventSource, sizeof(DM_ULP_CS_SUBEVENT_RESULT_CONTINUE_IND_T) + ind->stepsDataLen);
                CmLeCsSubeventResultRspSend();
#else /* !CSR_STREAMS_ENABLE */
                CsrPmemFree(ind->steps);
#endif /* CSR_STREAMS_ENABLE */
            break;
            }
#endif /* INSTALL_CHANNEL_SOUNDING_SUPPORT */
        default:
        {
            break;
        }
    }
}

#endif

